﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Mimoza.TestPlugin
{
    public class ExecutePlugin : Mimoza.PluginLayer.IExecutePlugin
    {
        #region IExecutePlugin Members

        public string PluginId
        {
            get { return this.ToString(); } 
        }

        public void Add(Mimoza.ObjectLayer.Policy policy)
        {
            if (policy.Data != null)
            {
                Add(GetData(policy.Data));
                return;
            }

            if (GetPolicyIndex(policy.ID) >= 0)
            {
                m_policies.Clear();
                throw new NotSupportedException();
            }

            m_policies.Add(policy);
        }

        public void Remove(Mimoza.ObjectLayer.Policy policy)
        {
            if (policy.Data != null)
            {
                Remove(GetData(policy.Data));
                return;
            }

            int index = GetPolicyIndex(policy.ID);
            if (index < 0)
            {
                m_policies.Clear();
                throw new NotSupportedException();
            }

            m_policies.RemoveAt(index);
        }

        public void Update(Mimoza.ObjectLayer.Policy policy)
        {
            if (policy.Data != null)
            {
                Update(GetData(policy.Data));
                return;
            }

            int index = GetPolicyIndex(policy.ID);
            if (index < 0)
            {
                m_policies.Clear();
                throw new NotSupportedException();
            }

            m_policies[index] = policy;
        }

        #endregion

        void Add(TestPluginData data)
        {
            string outStr = "Mimoza.TestPlugin.ExecutePlugin.Add Flag - '" + data.Flag.ToString() +
                "' Text - '" + data.Text + "'.";

            Trace(outStr);
        }

        void Update(TestPluginData data)
        {
            string outStr = "Mimoza.TestPlugin.ExecutePlugin.Update Flag - '" + data.Flag.ToString() +
                            "' Text - '" + data.Text + "'.";

            Trace(outStr);
        }

        void Remove(TestPluginData data)
        {
            string outStr = "Mimoza.TestPlugin.ExecutePlugin.Remove Flag - '" + data.Flag.ToString() +
                            "' Text - '" + data.Text + "'.";

            Trace(outStr);
        }

        void Trace(string outStr)
        {
            Common.Logger.Log.Info(outStr);
            System.Windows.Forms.MessageBox.Show(outStr);
        }

        public List<Mimoza.ObjectLayer.Policy> GetPoliciesTest()
        {
            return m_policies;
        }

        int GetPolicyIndex(Guid policyId)
        {
            return m_policies.FindIndex(p => p.ID == policyId);
        }

        TestPluginData GetData(string data)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(TestPluginData));
            using (TextReader text = new StringReader(data))
            {
                return deserializer.Deserialize(text) as TestPluginData;
            }
        }

        List<Mimoza.ObjectLayer.Policy> m_policies = new List<Mimoza.ObjectLayer.Policy>();
    }
}
